package com.i2bt.app.friend;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.Contract;

import com.i2bt.R;
import com.i2bt.adapter.FriendListAdapter;
import com.i2bt.app.BaseFragment;
import com.i2bt.app.friend.model.FriendJsonHandle;
import com.i2bt.bean.FriendBean;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.view.PopuItem;
import com.i2bt.view.PopuJar;
import com.i2bt.view.PopuJar.OnPopuItemClickListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class FriendFragment extends BaseFragment implements OnItemLongClickListener, 
OnPopuItemClickListener{
	
	private FriendListAdapter friendListAdapter;		//好友列表的适配器
	
	private List<FriendInfoBean> friendInfoList;		//好友信息链表
	
	private FriendJsonHandle friendJsonHandle;
	
	private PopuJar friendPopuMenu;		//好友按住弹出菜单
	
	private static int CONCERN_POPUMENU_ID = 1;		//取消关注弹出菜单项的id
	
	private int markPosition;
	
//	private static int SHARED_POPUMENU_ID = 2;		//分享弹出菜单项的id
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_friend, container, false);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 
		if (friendJsonHandle == null) {
			friendJsonHandle = new FriendJsonHandle(this);
		}
		makeData();
		showFriendRequest();
	}
	
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		
		if (isVisibleToUser) {
			
			if (friendInfoList != null && friendJsonHandle != null) {
				makeData();
				showFriendRequest();
			}

		}
		
		super.setUserVisibleHint(isVisibleToUser);
	}
	
	private void showFriendRequest() {
		FriendBean friendBean = new FriendBean();
		friendBean.setAct(AndroidRestContract.SHOW_FRIEND);
		friendJsonHandle.postTo(AndroidRestContract.REST_FRIEND, friendBean);
	}
	
	public void showFriendResponse(List<FriendInfoBean> friendInfoList) {
		this.friendInfoList = friendInfoList;
		friendListAdapter.setData(friendInfoList);
		friendListAdapter.notifyDataSetInvalidated();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
//		logic();
//		
//		makeData();
//		
		initView();
//		
//		showFriendRequest();
	}
	
	private void makeData() {
		friendInfoList = new ArrayList<FriendInfoBean>();
//		friendInfoList.add(new FriendInfoBean("我的名字叫卡卡", true, true));
//		friendInfoList.add(new FriendInfoBean("卡卡西", true, true));
//		friendInfoList.add(new FriendInfoBean("kakaxiisgood", true, true));
//		friendInfoList.add(new FriendInfoBean("无聊", true, true));
//		friendInfoList.add(new FriendInfoBean("大猩猩", true, true));
//		friendInfoList.add(new FriendInfoBean("金坷垃", true, true));
//		friendInfoList.add(new FriendInfoBean("aa", true, true));
//		friendInfoList.add(new FriendInfoBean("aa", true, true));
//		friendInfoList.add(new FriendInfoBean("aa", true, true));
	}
	
	@Override
	protected void logic() {

	}
	
	@Override
	protected void initView() {
		friendListAdapter = new FriendListAdapter(this, friendInfoList);
		
		listView = (ListView) activity.findViewById(R.id.friend_list);
		listView.setAdapter(friendListAdapter);
		listView.setOnItemClickListener(this);
		listView.setOnItemLongClickListener(this);
		
		//创建弹出式菜单的菜单项, 并设置点击自动关闭菜单
//		PopuItem sharedPopuItem = new PopuItem(SHARED_POPUMENU_ID, "一键分享");
//		sharedPopuItem.setSticky(true);
		PopuItem concernPopuItem = new PopuItem(CONCERN_POPUMENU_ID, "取消关注");
		concernPopuItem.setSticky(true);
		
		//把菜单项放入菜单栏里
		friendPopuMenu = new PopuJar(activity, PopuJar.HORIZONTAL);
		friendPopuMenu.setOnPopuItemClickListener(this);
//		friendPopuMenu.addPopuItem(sharedPopuItem);
		friendPopuMenu.addPopuItem(concernPopuItem);
	}
	
	@Override
	public void onItemClick(AdapterView<?> adapter, View view, 
			int position, long id) {
		//标记进入的好友
		markPosition = position;
		//转到好友详情并携带参数
		Bundle bundle = new Bundle();
		bundle.putSerializable(Contract.BUNDLE_FRIEND_INFO, friendInfoList.get(position));
		goActivityAndResult(FriendDetailActivity.class, bundle, Contract.FRIEND_REQUESTCODE);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> adapter, View view, 
			int position, long id) {
		// 位置标记
		markPosition = position;
		// 在长按的列表项上显示出弹出式菜单
		friendPopuMenu.show(view);
		return false;
	}
	
	@Override
	public void onItemClick(PopuJar source, int pos, int actionId) {
		deleteFriendRequest();
	}
	
	/**
	 * 取消好友关注请求
	 */
	private void deleteFriendRequest() {
		FriendBean friendBean = new FriendBean();
		friendBean.setAct(AndroidRestContract.DELETE_FRIEND);
		friendBean.setFriendId(friendInfoList.get(markPosition).getFriendId());
		friendJsonHandle.postTo(AndroidRestContract.REST_FRIEND, friendBean);
	}
	
	/**
	 * 取消好友关注响应
	 */
	public void deleteFriendResponse() {
		//删除
		friendInfoList.remove(markPosition);
		friendListAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		switch (requestCode) {
		
		case Contract.FRIEND_REQUESTCODE : {
			
		}
		
		}
	}

	@Override
	public boolean onKeyBack() {
		return false;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.clear();
		inflater.inflate(R.menu.friend_menu, menu);
//		menu.setGroupVisible(R.id.group_files_action, false);
//		menu.setGroupVisible(R.id.group_friend_action, true);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		
		case R.id.action_add_friend : {
			System.out.println("hehe");
			goActivity(FriendAddActivity.class);
			break;
		}
		
		}
		
		return true;
	}
	
	@Override
	public void onClick(View v) {}
	
	@Override
	public void backHomeOperate() {}
	
}
